package com.irdstudio.efp.esb.service.impl.loan;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.constant.ESBConstant;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.basic.framework.core.util.TraceUtil;
import com.irdstudio.efp.esb.common.client.resp.EsbRespServiceBean;
import com.irdstudio.efp.esb.common.constant.ESBClientConstance;
import com.irdstudio.efp.esb.common.constant.EsbBizEnums;
import com.irdstudio.efp.esb.service.bo.req.loan.PerLoanSysPrdInfoLprReqBean;
import com.irdstudio.efp.esb.service.bo.resp.loan.PerLoanSysPrdInfoLprRespBean;
import com.irdstudio.efp.esb.service.client.ESBRequestBuilder;
import com.irdstudio.efp.esb.service.client.EsbBaseBean;
import com.irdstudio.efp.esb.service.facade.loan.PerLoanSysPrdInfoLprService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service("perLoanSysPrdInfoLprService")
public class PerLoanSysPrdInfoLprServiceImpl implements PerLoanSysPrdInfoLprService {

    private static Logger logger = LoggerFactory.getLogger(PerLoanSysPrdInfoLprServiceImpl.class);

    @Override
    public PerLoanSysPrdInfoLprRespBean applyPerLoanSysPrdInfoLpr(PerLoanSysPrdInfoLprReqBean perLoanSysPrdInfoLprReqBean) throws ESBException {
        logger.info("======>调用个贷利率信息查询接口【" + TraceUtil.getGlblSrlNo() + "_30230003_18】开始<======");
        PerLoanSysPrdInfoLprRespBean perLoanSysPrdInfoLprRespBean = null;
        try {
            EsbBaseBean esbBaseBean = EsbBaseBean.EsbBaseBeanBuilder.
                    anEsbBaseBean().
                    withINSTID(ESBClientConstance.bdInstId).withGLBLSRLNO(TraceUtil.getGlblSrlNo()).build();
            EsbRespServiceBean recvBean = ESBRequestBuilder.buildClient().
                    withEsbBaseBean(esbBaseBean)
                    .withBody(perLoanSysPrdInfoLprReqBean)
                    .withTradeNo(ESBConstant.TRADE_CODE_30230003)
                    .withScene(ESBConstant.SCENE_NO_18)
                    .create()
                    .sendAndReceive();
            if (EsbBizEnums.RetCodeEnum.SUCCESS.VALUE.equals(recvBean.getSysHead().getRetInfArry().get(0).getRetCd())) {
                perLoanSysPrdInfoLprRespBean = recvBean.getBody(PerLoanSysPrdInfoLprRespBean.class);
            } else {
                throw new ESBException(recvBean.getSysHead().getRetInfArry().get(0).getRetMsg());
            }
            logger.info("调用个贷利率信息查询接口【30230003_18】结束，返回信息：" + JSONObject.toJSONString(perLoanSysPrdInfoLprRespBean));
        } catch (Exception e) {
            logger.error("调用个贷利率信息查询接口【30230003_18】出现异常：" + e.getMessage());
            throw new ESBException("调用个贷利率信息查询接口【30230003_18】出现异常：" + e.getMessage());
        } finally {
            logger.info("======>调用个贷利率信息查询接口【" + TraceUtil.getGlblSrlNo() + "_30230003_18】结束<======");
        }
        return perLoanSysPrdInfoLprRespBean;
    }
}